//
//  JXAdviseViewController.m
//  JXAddressApp
//
//  Created by huawei.zheng on 2018/11/22.
//  Copyright © 2018 zhhw_home. All rights reserved.
//

#import "JXAdviseViewController.h"
#import "UIColor+QMUI.h"
#import <QMUIKit/QMUICommonDefines.h>
#import "EgovaNetworking.h"
#import "QMUITips.h"

@interface JXAdviseViewController ()

@property (nonatomic, strong)   UITextField  *tf1;
@property (nonatomic, strong)   UITextField  *tf2;
@property (nonatomic, strong)   UITextView  *tview;

@end

@implementation JXAdviseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"政策咨询";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.extendedLayoutIncludesOpaqueBars = YES;
    
    [self initRootView];
}

- (void)initRootView
{
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel *tl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 88, 80, 44)];
    tl1.textColor = [UIColor qmui_colorWithHexString:@"#333333"];
    tl1.font = [UIFont systemFontOfSize:14];
    tl1.textAlignment = NSTextAlignmentRight;
    tl1.text = @"姓名:";
    [self.view addSubview:tl1];
    
    _tf1 = [[UITextField alloc] initWithFrame:CGRectMake(90, 88, SCREEN_WIDTH-90, 44)];
    _tf1.textColor = [UIColor qmui_colorWithHexString:@"#333333"];
    _tf1.font = [UIFont systemFontOfSize:14];
    _tf1.textAlignment = NSTextAlignmentLeft;
    _tf1.placeholder = @"您的姓名";
    [self.view addSubview:_tf1];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 131.5, SCREEN_WIDTH, 0.5f)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:lineView];
    
    UILabel *tl2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 132, 80, 44)];
    tl2.textColor = [UIColor qmui_colorWithHexString:@"#333333"];
    tl2.font = [UIFont systemFontOfSize:14];
    tl2.textAlignment = NSTextAlignmentRight;
    tl2.text = @"电子邮箱:";
    [self.view addSubview:tl2];
    
    _tf2 = [[UITextField alloc] initWithFrame:CGRectMake(90, 132, SCREEN_WIDTH-90, 44)];
    _tf2.textColor = [UIColor qmui_colorWithHexString:@"#333333"];
    _tf2.font = [UIFont systemFontOfSize:14];
    _tf2.textAlignment = NSTextAlignmentLeft;
    _tf2.placeholder = @"您的电子邮件";
    [self.view addSubview:_tf2];
    
    lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 175.5, SCREEN_WIDTH, 0.5f)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:lineView];
    
    UILabel *tl3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 176, 80, 130)];
    tl3.textColor = [UIColor qmui_colorWithHexString:@"#333333"];
    tl3.font = [UIFont systemFontOfSize:14];
    tl3.textAlignment = NSTextAlignmentRight;
    tl3.text = @"咨询问题:";
    [self.view addSubview:tl3];
    
    _tview = [[UITextView alloc] initWithFrame:CGRectMake(80, 176, SCREEN_WIDTH-80, 130)];
    _tview.textColor = [UIColor qmui_colorWithHexString:@"#333333"];
    _tview.font = [UIFont systemFontOfSize:14];
    _tview.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:_tview];
    
    lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 305.5, SCREEN_WIDTH, 0.5f)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:lineView];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(commit)];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)commit
{
    NSString *name = _tf1.text;
    NSString *email = _tf2.text;
    NSString *problem = _tview.text;
    
    if (name.length < 1) {
        [QMUITips showWithText:@"姓名不能为空" inView:self.view hideAfterDelay:1.5f];
        return;
    }
    
    if (email.length < 1) {
        [QMUITips showWithText:@"邮箱不能为空" inView:self.view hideAfterDelay:1.5f];
        return;
    }
    
    if (problem.length < 1) {
        [QMUITips showWithText:@"咨询问题不能为空" inView:self.view hideAfterDelay:1.5f];
        return;
    }
    
    NSDate *dt = [NSDate new];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSString *dateTime = [formatter stringFromDate:dt];
    
    NSDictionary *params = @{@"problem":@3,
                             @"cname":name,
                             @"cemail":email,
                             @"cproblem":problem,
                             @"agree":@(1),
                             @"ctime":dateTime
                             };
    NSString *url = @"http://wegis.net/demo/home/baseinfo/alterpolicyreply";
    
    [EgovaNetworking getWithUrl:url cache:NO params:params completed:^(NSInteger code, id data, NSString *msg) {
        if (code == 0) {
            [QMUITips showSucceed:@"提交成功！" inView:self.view hideAfterDelay:1.5f];
            [self performSelector:@selector(dismiss) withObject:nil afterDelay:1.5];
        }
        else {
            [QMUITips showWithText:msg?:@"提交失败，请稍后重试!" inView:self.view hideAfterDelay:2.5f];
        }
    }];
}

- (void)dismiss
{
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
